<template>
  <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option v-for="item in peoples" :key="item.id" :value="item.username" :label="item.username" />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
import { getEmployeeSimple } from '@/api/employees'
export default {
  props: {
    editId: {
      type: [String, Number],
      required: false,
      default: ''
    },
    addId: {
      type: [String, Number],
      required: false,
      default: ''
    }
  },
  data() {
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: ['blur', 'change'] }
        ]
      },
      peoples: []
    }
  },
  watch: {
    editId(newVal, oldVal) {
      console.log('editId', newVal, oldVal)
    }
  },
  mounted() {
    this.editId && this.loadDepartDetail(this.editId)
    this.loadEmployeeSimple()
  },
  methods: {
    hCancel() {
      this.$emit('close')
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      // 清除校验残留
      this.$refs.deptForm.resetFields()
    },
    // 获取员工简单列表数据
    async loadEmployeeSimple() {
      const data = await getEmployeeSimple()
      this.peoples = data
    },
    async loadDepartDetail(id) {
      const res = await getDepartDetail(id)
      this.form = res
    },

    addOrEdit() {
      if (this.addId) {
        this.add()
      } else {
        this.edit()
      }
    },
    async add() {
      await addDepartments({ ...this.form, pid: this.addId })
      this.hCancel()
      this.$emit('update-depart')
    },
    async edit() {
      await updateDepartments({ ...this.form, pid: this.editId })
      this.hCancel()
      this.$emit('update-depart')
    },
    hSubmit() {
      this.$refs.deptForm.validate(async valid => {
        if (valid) {
          this.addOrEdit()
        }
      })
    }
  }
}
</script>

<style>

</style>
